package com.wn.budget.controller;

import com.wn.budget.model.ProjectMaterial;
import com.wn.budget.repository.ProjectMaterialRepository;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

/**
 * 工程材料
 */
@RestController
@RequestMapping(value = "projectMaterial")
public class ProjectMaterialController {
    @Autowired
    ProjectMaterialRepository projectMaterialRepository;

    /**
     * 给空间新增材料
     */
    @PostMapping("insert")
    public boolean insert(HttpServletRequest request, ProjectMaterial project) {
        long exist = projectMaterialRepository.countByProjectIdAndProjectSpaceIdAndMaterialId(project.getProjectId(), project.getProjectSpaceId(), project.getMaterialId());
        if (exist > 0) {
            throw new RuntimeException("材料已经存在，不能新增，请修改数量即可。");
        }
        project.setId(UUID.randomUUID().toString());
        project.setCreateDate(new Date());
        projectMaterialRepository.saveAndFlush(project);
        return true;
    }

    /**
     * 更新材料用量
     *
     * @param id       ProjectMaterial ID
     * @param quantity 数量
     * @return
     */
    @GetMapping("updateProjectMaterial")
    @ApiOperation("更新材料用量")
    public boolean updateProjectMaterial(@RequestParam("id") String id, @RequestParam("quantity") int quantity) {
        ProjectMaterial projectMaterial = projectMaterialRepository.getOne(id);
        if (projectMaterial != null) {
            projectMaterial.setQuantity(quantity);
            projectMaterialRepository.saveAndFlush(projectMaterial);
        }
        return true;
    }

    @GetMapping("findById")
    public ProjectMaterial findById(@RequestParam("id") String id) {
        return projectMaterialRepository.findById(id).get();
    }

    @GetMapping("deleteById")
    public void deleteById(@RequestParam("id") String id) {
        projectMaterialRepository.deleteById(id);
    }

    /**
     * 根据工程id和空间id查询工程材料
     *
     * @param request        请求
     * @param projectId      工程id
     * @param projectSpaceId 空间id
     * @param pageIndex      index
     * @param pageSize       size
     * @return
     */
    @GetMapping("findAll")
    public Page<ProjectMaterial> findAll(HttpServletRequest request,
                                         @RequestParam("projectId") String projectId,
                                         @RequestParam("projectSpaceId") String projectSpaceId,
                                         @RequestParam("pageIndex") int pageIndex,
                                         @RequestParam("pageSize") int pageSize) {

        Page<ProjectMaterial> page = projectMaterialRepository.findByProjectIdAndProjectSpaceId(projectId, projectSpaceId, PageRequest.of(pageIndex, pageSize));

        return page;
    }
}
